$(document).ready(function() {
    $.fn.dataTableExt.sErrMode = 'throw';
    reload_list_user();
    getKewenangan();
    getKewenanganPIC();

    // $.getJSON('controller/cont.login.php',{aksi_isAdmin:true},function(data){
    //     if(data.msg){
    //         getAllKanwil();
    //         $('#div_kanwil').css({
    //             'display' : 'block',
    //         });
    //     }else{
    //         getKanwil()
    //     }
    // });

$("#btn_cari").click(function(){
    var nip=$("#nip_cari").val();
    if(nip===""){
        alert("Isikan NIP User Dahulu");
    }else{
        reload_user(nip);
    }
    return false;
});

    $('#btn_simpan').click(function(e) {
        e.preventDefault();
        var nip = $('#nip').val();
        var password = $('#password').val();
        var password2 = $('#password2').val();
        var f = document.getElementById('kanwil');
        var kanwil = f.options[f.selectedIndex].value;

        var e = document.getElementById('kewenangan');
        var kewenangan = e.options[e.selectedIndex].value;

        if (nip === '' || password === '' || password2 === '') {
            $('#error').html("Semua Field Harus Diisi!").fadeIn(500).delay(2500).fadeOut(500);
        } else if (password !== password2) {
            $('#error').html("Konfirmasi Password Tidak Sama").fadeIn(500).delay(2500).fadeOut(500);
        }else if(password.length < 6){
            $('#error').html("Password Minimal 6 karakter").fadeIn(500).delay(2500).fadeOut(500);
        } else {

            $.post('controller/cont.ruh.php', {aksi_rekam_user: true, nip: nip, password: password, id_kewenangan: kewenangan,kdkanwil:kanwil}, function(data) {
                if (data.msg) {
                    $('#success').html('Data Berhasil Direkam').fadeIn(500).delay(2500).fadeOut(500);
                    clear_input();
                } else {
                    $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                }

            }, 'json');
        }

        return false;
    });

    $('#btn_simpan_ubah').click(function(e) {
        e.preventDefault();
        var id_user = $('#id_user').val();
        var nip = $('#nip').val();
        var password = $('#password').val();
        var password2 = $('#password2').val();

        var f = document.getElementById('kanwil');
        var kanwil = f.options[f.selectedIndex].value;

        var e = document.getElementById('kewenangan');
        var kewenangan = e.options[e.selectedIndex].value;

        if (nip === '' || password === '' || password2 === '') {
            $('#error').html("NIP dan Password tidak boleh kosong").fadeIn(500).delay(2500).fadeOut(500);
        } else if (password !== password2) {
            $('#error').html("Konfirmasi Password Salah").fadeIn(500).delay(2500).fadeOut(500);
        } else {
            $.post('controller/cont.ruh.php', {aksi_simpan_ubah_user: true, id_user: id_user, nip: nip,
                password: password, id_kewenangan: kewenangan, kdkanwil:kdkanwil}, function(data) {
                if (data.msg) {
                    $('#success').html('Data Berhasil Diubah').fadeIn(500).delay(2500).fadeOut(500);
                    clear_input();
                } else {
                    $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                }
            }, 'json');
        }
        return false;
    });

    $("#btn_simpan_pic").click(function(){
        var id_user = $('#id_user2').val();
        var e = document.getElementById('kewenangan_pic');
        var kewenangan = e.options[e.selectedIndex].value;

        $.getJSON('controller/cont.ruh.php',{aksi_simpan_pic:true,id:id_user,kewenangan:kewenangan},function(data){
            if(data.msg){
                alert("Berhasil Ubah Kewenangan PIC");
            }else{
                alert("Gagal Ubah Kewenangan PIC");
            }
        });
        return false;
    });
    $("#nip").keyup(function() {
        var val=$('#nip').val();
        if(val.length===18){
            $.getJSON('controller/cont.pegawai.php',{aksi_biodata:true,nip:val},function(data){
                if(data.msg){
                    document.getElementById('nip_td').innerHTML=data.nip;
                    document.getElementById('nama').innerHTML=data.nama;
                    document.getElementById('gol').innerHTML=data.golongan+" / "+data.pangkat;
                    document.getElementById('eselon').innerHTML=data.eselon;
                    $('#biodata').fadeIn(500).delay(10000).fadeOut(500);
                }else{
                    document.getElementById('biodata').innerHTML='NIP Tidak Ada di Daftar';
                    $('#biodata').fadeIn(500).delay(10000).fadeOut(500);
                }
                
            });
        }   
    });

    
    return false;
});

function getKewenanganPIC(){
    $.getJSON('controller/cont.ruh.php',{aksi_get_all_kewenangan:true},function(data){
        $.each(data, function(index,data){
            var option2 = $('<option />');
            option2.attr('value', data.id_kewenangan).text(data.nama_kewenangan); 
            $('#kewenangan_pic').append(option2);
        });
    });
}

function getKewenangan(){
    $.getJSON('controller/cont.ruh.php',{aksi_get_all_kewenangan:true},function(data){
        $.each(data, function(index,data){
            var option2 = $('<option />');
            option2.attr('value', data.id_kewenangan).text(data.nama_kewenangan); 
            $('#kewenangan').append(option2);
        });
    });
}

function getAllKanwil(){
    $.getJSON('controller/cont.kanwil.php',{aksi_get_all_kanwil:true},function(data){
        $.each(data, function(index,data){
            var option = $('<option />');
            option.attr('value', data.kdkanwil).text(data.nmkanwil); 
            $('#kanwil').append(option);
        });
    });
}

function getKanwil(){
    $.getJSON('controller/cont.kanwil.php',{aksi_get_kanwil:true},function(data){
            var option = $('<option />');
            option.attr('value', data.kdkanwil).text(data.nmkanwil); 
            $('#kanwil').append(option);
    });
}

function hapus(id_user) {
    $.getJSON('controller/cont.ruh.php', {aksi_hapus_user: true, id_user: id_user}, function(data) {
        if (data.msg) {
            alert("User berhasil dihapus");
            reload_list_user();
        } else {
            $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        }
    });
}

function reload_list_user() {
    $('#loader').fadeIn(500);
    $.getJSON('controller/cont.list.php', {list_user: true}, function(data) {
        $('#list_users tbody').html('');
        if(jQuery.isEmptyObject(data)){
            $('#list_users tbody').append('<td colspan="8">Tidak Ada Data</td>');
        }else{
            
                var aktif = '';
                var statusnya = 'Tidak Aktif';

                if (data.status_aktif == 1) {
                    aktif = 'disabled';
                    statusnya = 'Aktif';
                }
                // var nip = '';
                // if (!data.id_parent_user) {
                //     nip = '-';
                // } else {
                //     nip = data.id_parent_user;
                // }

                var tab = "<tr>";
                tab += "<td>" + data.nip + "</td>";
                //tab += "<td>" + nip + "</td>";
                //tab += "<td>" + data.kanwil + "</td>";
                tab += "<td>" + data.id_kewenangan + "</td>";
                tab += "<td>" + statusnya + "</td>";
                // tab += "<td align=\"center\"><button type=\"button\" onclick=\"hapus(" + data.id_user + ")\" class=\"btn btn-default btn-sm\"><span class=\"glyphicon glyphicon-remove-circle\"></span></button></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" onclick=\"ubah(" + data.id_user + ")\" class=\"btn btn-default btn-sm\"><span class=\"glyphicon glyphicon-edit\"></span></a></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" onclick=\"aktivasi(" + data.id_user + ")\" class=\"btn btn-default btn-sm\" " + aktif + "><span class=\"glyphicon glyphicon-saved\"></span>Aktivasi</a></td>";
                tab += "</tr>";
                //alert(tab);
                $('#list_users tbody').append(tab);

        }
        // $('#list_users').dataTable({
        //     "sPaginationType": "bs_full",
        //     "bInfo": false,
        //     "bLengthChange": false,
        //     "iDisplayLength": 5,
        //   }); 
        //   $('#list_users').each(function(){
        //     var datatable = $(this);
        //     // SEARCH - Add the placeholder for Search and Turn this into in-line form control
        //     var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
        //     search_input.attr('placeholder', 'Search');
        //     search_input.addClass('form-control input-sm');
        //     // LENGTH - Inline-Form control
        //     var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
        //     length_sel.addClass('form-control input-sm');
        //   });
    });
    $('#loader').fadeOut(500);

}

function reload_user(nip) {
    $('#loader').fadeIn(500);
    $.getJSON('controller/cont.list.php', {data_user: true,nip: nip}, function(data) {
        $('#list_users tbody').html('');
        if(jQuery.isEmptyObject(data)){
            $('#list_users tbody').append('<td colspan="8">Tidak Ada Data</td>');
        }else{
            
                var aktif = '';
                var statusnya = 'Tidak Aktif';
                var non_aktif='disabled';

                if (data.status_aktif == 1) {
                    aktif = 'disabled';
                    non_aktif='';
                    statusnya = 'Aktif';
                }
                // var nip = '';
                // if (!data.id_parent_user) {
                //     nip = '-';
                // } else {
                //     nip = data.id_parent_user;
                // }

                var tab = "<tr>";
                tab += "<td>" + data.nip + "</td>";
                //tab += "<td>" + nip + "</td>";
                //tab += "<td>" + data.kanwil + "</td>";
                tab += "<td>" + data.id_kewenangan + "</td>";
                tab += "<td>" + statusnya + "</td>";
                // tab += "<td align=\"center\"><button type=\"button\" onclick=\"hapus(" + data.id_user + ")\" class=\"btn btn-default btn-sm\"><span class=\"glyphicon glyphicon-remove-circle\"></span></button></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" onclick=\"ubah(" + data.id_user + ")\" class=\"btn btn-default btn-sm\"><span class=\"glyphicon glyphicon-edit\"></span>Ubah</a><a data-toggle=\"modal\" onclick=\"ubahPIC(" + data.id_user + ")\" class=\"btn btn-default btn-sm\"><span class=\"glyphicon glyphicon-edit\">Ubah PIC</span></a></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" onclick=\"aktivasi(" + data.id_user + ")\" class=\"btn btn-default btn-sm\" " + aktif + "><span class=\"glyphicon glyphicon-saved\"></span>Aktivasi</a><a data-toggle=\"modal\" onclick=\"deaktivasi(" + data.id_user + ")\" class=\"btn btn-default btn-sm\" " + non_aktif + "><span class=\"glyphicon glyphicon-saved\"></span>Deaktivasi</a></td>";
                tab += "</tr>";
                //alert(tab);
                $('#list_users tbody').append(tab);

        }
    });
    $('#loader').fadeOut(500);

}

function clear_input() {
    $('#judul_modal').html('Rekam Data');
    $('#btn_simpan_ubah').hide();
    $('#btn_simpan').show();
    document.getElementById('nip').value = "";
    document.getElementById('password').value = "";
    document.getElementById('password2').value = "";
}

function ubah(id_user) {
    $('#myModal').modal('toggle');
    $('#judul_modal').html('Ubah Data');
    $('#btn_simpan_ubah').show();
    $('#btn_simpan').hide();

    $.getJSON('controller/cont.ruh.php', {aksi_get_user_ubah: true, id_user: id_user}, function(data) {
        document.getElementById('id_user').value = data.id_user;
        document.getElementById('nip').value = data.nip;
        document.getElementById('password').value = "";
        document.getElementById('password2').value = "";

        var ddl = document.getElementById('kewenangan');
        var opts = ddl.options.length;
        for (var i = 0; i < opts; i++) {
            if (ddl.options[i].value == data.id_kewenangan) {
                ddl.options[i].selected = true;
                break;
            }
        }

        var ddl2 = document.getElementById('kanwil');
        var opts2 = ddl2.options.length;
        for (var i = 0; i < opts2; i++) {
            if (ddl2.options[i].value == data.kdkanwil) {
                ddl2.options[i].selected = true;
                break;
            }
        }
        return false;
    });

}

function ubahPIC(id_user) {
    $('#myModalPIC').modal('toggle');
    $('#judul_modal').html('Ubah PIC');
    $('#btn_simpan').show();

    $.getJSON('controller/cont.ruh.php', {aksi_get_user_pic: true, id_user: id_user}, function(data) {
        document.getElementById('id_user2').value = data.id_user;
        $('#nip_td2').html(data.nip);
        $('#nama2').html(data.pegawai.nama);
         $('#gol2').html(data.gol.pangkat+" ("+data.gol.nmgol+")");


        var ddl = document.getElementById('kewenangan_pic');
        var opts = ddl.options.length;
        for (var i = 0; i < opts; i++) {
            if (ddl.options[i].value == data.id_kewenangan) {
                ddl.options[i].selected = true;
                break;
            }
        }

        return false;
    });

}

function aktivasi(id_user) {
    $.getJSON('controller/cont.ruh.php', {aksi_aktivasi_user: true, id_user: id_user}, function(data) {
        if (data.msg) {
            alert("User berhasil diaktivasi");
            reload_list_user();
        } else {
            $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        }
    });
}
function deaktivasi(id_user) {
    $.getJSON('controller/cont.ruh.php', {aksi_deaktivasi_user: true, id_user: id_user}, function(data) {
        if (data.msg) {
            alert("User berhasil diaktivasi");
            reload_list_user();
        } else {
            $('#error').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        }
    });
}